package GUI;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ClientConsole;

import java.awt.event.ActionListener;

/**
 * 
 * adding new reader card
 * @author Maor
 *
 */
public class GUI_AddReaderCard extends JPanel {

	private JTextField textField_1;
	/**
	 * phone text field
	 */
	private JTextField textField_2;
	/**
	 * first name text field
	 */
	private JTextField textField_3;
	/**
	 * last name text field
	 */
	private JTextField textField_4;
	/**
	 * email text field
	 */
	private JTextField textField_5;
	/**
	 * client console
	 */
	private ClientConsole clc;
	/**
	 * panel that we back to him when we press 'back'
	 */
	private JPanel backPanel;
	/**
	 * main frame window
	 */
	private GUI_LogIn gl;
	
	
	/**
	 * constructor that called from librerian menu, when the librerian want to add  new reader card
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param lm  libreian menu, for back
	 * @wbp.parser.constructor
	 */
	public GUI_AddReaderCard(ClientConsole cc, final GUI_LogIn gl,final GUI_LibrarianMenu lm) {
		
		clc=cc;
		this.gl=gl;
		backPanel=lm;
		setLayout(null);
		this.setSize(700, 600);
		same();
	}
	
	/**
	 * 
	* constructor that called from Manager menu, when the Manager want to add  new reader card
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param mm  Manager menu, for back
	 */
	public GUI_AddReaderCard(ClientConsole cc, final GUI_LogIn gl,final GUI_ManagerMenu mm) {
		
		clc=cc;
		this.gl=gl;
		backPanel=mm;
		setLayout(null);
		this.setSize(700, 600);
		same();
	}
	
	/**
	 * same things in window for manager and librerian both
	 * when the user fill all the fields and press 'submit'  the method
	 * call to server for insert the new reader card
	 */
	public void same()
	{
		JLabel lblPleaseFillAll = new JLabel("Please fill all the columns");
		lblPleaseFillAll.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblPleaseFillAll.setBounds(39, 50, 117, 14);
		add(lblPleaseFillAll);
		
		JLabel lblReaderCardFill = new JLabel("Reader card fill");
		lblReaderCardFill.setFont(new Font("Tahoma", Font.PLAIN, 24));
		lblReaderCardFill.setBounds(178, 11, 173, 23);
		add(lblReaderCardFill);
		
		JLabel lblUserName = new JLabel("user name");
		lblUserName.setBounds(39, 107, 66, 14);
		add(lblUserName);
		
		textField_1 = new JTextField();
		textField_1.setBounds(125, 104, 86, 20);
		add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblPhone = new JLabel("phone");
		lblPhone.setBounds(39, 138, 46, 14);
		add(lblPhone);
		
		textField_2 = new JTextField();
		textField_2.setBounds(125, 135, 86, 20);
		add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("First Name");
		lblNewLabel.setBounds(39, 170, 66, 14);
		add(lblNewLabel);
		
		textField_3 = new JTextField();
		textField_3.setBounds(125, 167, 86, 20);
		add(textField_3);
		textField_3.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setBounds(125, 202, 86, 20);
		add(textField_4);
		textField_4.setColumns(10);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(39, 205, 66, 14);
		add(lblLastName);
		
		textField_5 = new JTextField();
		textField_5.setBounds(125, 235, 86, 20);
		add(textField_5);
		textField_5.setColumns(10);
		
		JLabel lblEmail = new JLabel("E-mail");
		lblEmail.setBounds(39, 238, 46, 14);
		add(lblEmail);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			//	String ReaderId=textField.getText();
				 String userName=textField_1.getText();
				 String phone=textField_2.getText();
				 String FirstName=textField_3.getText();
				 String LastName=textField_4.getText();
				 String email=textField_5.getText();
				 String[] s={"new reader card",userName,phone,FirstName,LastName,email};
				 try {
				 if(userName.equals("")||phone.equals("")||FirstName.equals("")||LastName.equals("")||email.equals(""))
					 JOptionPane.showMessageDialog(new JFrame(),"there are empty fields");
				 else{
					clc.client.sendToServer(s);
					while(clc.client.flag==0)
						System.out.println(" ");
					clc.client.flag=0;
					JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
					textField_1.setText(null);
					textField_2.setText(null);
					textField_3.setText(null);
					textField_4.setText(null);
					textField_5.setText(null);
				 }
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			});

		btnSubmit.setBounds(139, 311, 89, 23);
		add(btnSubmit);
		
		JButton btnBack = new JButton("Back");
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(backPanel);
			}
		})
		;
		btnBack.setBounds(262, 311, 89, 23);
		add(btnBack);
	
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(410, 311, 89, 23);
		add(btnLogOut);
	}
}
